package com.byedu.course.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Luo on 2017-08-24.
 */
public class LoginFilter implements Filter {
    public void destroy() {
        System.out.println("LoginFilter destroy");
    }


    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        HttpServletRequest request =(HttpServletRequest)req;
        HttpServletResponse response =(HttpServletResponse)resp;

        String url = request.getRequestURL().toString();
        System.out.println("url:"+url);
        if(url.contains("css") || url.contains("js") || url.contains("images")|| url.contains("file")){

        //如果发现是css或者js文件，直接放行
            chain.doFilter(request, response);
            return;
        }
//        if(url.contains("login") ){
//            //如果发现是登陆页面
//            chain.doFilter(request, response);
//            return;
//        }
//        System.out.println("LoginFilter doFilter"+request.getRequestURL());
//        //判断用户用户是否登陆
//        FocusUserVO focusUserDO = (FocusUserVO)request.getSession().getAttribute("user");
//
//        if(focusUserDO == null){
//        //读取Cookie缓存
//          String autoUser = CookieUtil.get("user",request);
//           if(autoUser != null){
//            //获取是否选择自动登录
//            //调用登陆业务校验是否合法
//               String username = URLDecoder.decode(autoUser.split("-")[0], "UTF-8");
//               String password =URLDecoder.decode(autoUser.split("-")[1], "UTF-8");
//
//               FocusUserDO initUser = new FocusUserDO();
//               initUser.setUsername(username);
//               initUser.setPassword(password);
//               FocusUserVO checkUser = focusUserService.getFocusUser(initUser);
////               System.out.println(checkUser);
//               if(checkUser == null){
//                   //回到登陆页面
//                   response.sendRedirect(request.getContextPath() + "/user/login");
//                   return;
//               }
//               request.getSession().setAttribute("user",checkUser);
//           }else{
//               //回到登陆页面
//               response.sendRedirect(request.getContextPath() + "/user/login");
//               return;
//           }
//
//       }else{
//            if(StringUtil.isNullOrEmpty(focusUserDO.getId())) {
//                response.sendRedirect(request.getContextPath() + "/user/login");
//                return;
//            }
//        }
           chain.doFilter(request, response);

    }

    public void init(FilterConfig config) throws ServletException {
        ServletContext servletContext = config.getServletContext();
//        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//        focusUserService = (FocusUserService)ctx.getBean("focusUserService");

        System.out.println("LoginFilter init");
    }

}
